<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品" prop="goodId">
        <el-select
          v-model="queryParams.goodId"
          placeholder="请选择报损商品"
          filterable
          clearable
          size="small"
          style="width: 270px"
        >
          <el-option
            v-for="dict in queryGoodList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="仓库" prop="warehouseId">
        <el-select
          v-model="queryParams.warehouseId"
          placeholder="请选择报损仓库"
          filterable
          clearable
          @change="check"
          size="small"
          style="width: 270px"
        >
          <el-option
            v-for="dict in warehouseList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建人" prop="createUser">
        <el-input
          v-model="queryParams.createUser"
          placeholder="请输入创建人"
          clearable
          maxlength="20"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:reporting:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reportingList">
      <el-table-column label="商品" align="center" prop="goodName" />
      <el-table-column label="仓库" align="center" prop="warehouseName" />
      <el-table-column label="损坏数量" align="center" prop="damageNum" />
      <el-table-column label="报损人" align="center" prop="createUser" />
      <el-table-column label="报损原因" align="center" prop="reason" show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="type" :formatter="typeCheck"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.type == 1"
            v-hasPermi="['system:reporting:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.type != 0"
            @click="confirmReporting(scope.row)"
          >确认</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.type == 1"
            v-hasPermi="['system:reporting:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报损单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仓库" prop="warehouseId">
          <el-select
            v-model="form.warehouseId"
            placeholder="请选择报损仓库"
            clearable
            filterable
            @change="check"
            size="medium"
            style="width: 380px"
          >
            <el-option
              v-for="dict in warehouseList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="报损商品" prop="goodId">
          <el-select
            v-model="form.goodId"
            placeholder="请选择报损商品"
            clearable
            filterable
            :disabled="this.form.warehouseId == null"
            size="medium"
            style="width: 380px"
          >
            <el-option
              v-for="dict in goodsList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="损坏数量" prop="damageNum">
          <el-input-number v-model="form.damageNum" controls-position="right"
                           :min="1" size="medium" style="width:380px"
          />
        </el-form-item>
        <el-form-item label="报损人" prop="createUser">
          <el-input v-model="form.createUser" placeholder="请输入报损人"  maxlength="40"/>
        </el-form-item>
        <el-form-item label="报损原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入报损原因" maxlength="240"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listReporting,
  getReporting,
  delReporting,
  addReporting,
  updateReporting,
  confirmReporting
} from "@/api/erp/reporting";
import {listWarehouse} from "@/api/erp/warehouse";
import {listGoods, warehouseList} from "@/api/erp/goods";

export default {
  name: "Reporting",
  data() {
    return {
      //仓库list
      warehouseList:[],
      //仓库商品list
      goodsList:[],
      //查询条件商品List
      queryGoodList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报损单表格数据
      reportingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodId: null,
        warehouseId: null,
        damageNum: null,
        createUser: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        warehouseId: [
          { message: "报损仓库不能为空", trigger: "blur", required: true}
        ],
        goodId: [
          { message: "报损商品不能为空", trigger: "blur", required: true}
        ],
        damageNum: [
          { message: "报损数量不能为空", trigger: "blur", required: true}
        ],
        createUser: [
          { message: "报损人不能为空", trigger: "blur", required: true}
        ],
        reason: [
          { message: "报损原因不能为空", trigger: "blur", required: true}
        ],
      }
    };
  },
  created() {
    this.getList();
   this.getQueryGoodList();
    this.getWarehouse();
  },
  methods: {
    /** 查询报损单列表 */
    getList() {
      this.loading = true;
      listReporting(this.queryParams).then(response => {
        this.reportingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /*查询仓库*/
    getWarehouse() {
      listWarehouse().then(response => {
        this.warehouseList = response.rows;
      });
    },
    check(){
      if (this.form.warehouseId != null){
        this.getGoodsList();
      }
    },
    getQueryGoodList(){
      listGoods().then(res =>{
        this.queryGoodList = res.rows
      })
    },

    //查询商品
    getGoodsList(){
      warehouseList(this.form).then(response => {
        this.goodsList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodId: null,
        warehouseId: null,
        damageNum: null,
        createUser: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.getWarehouse();
      this.reset();
      this.open = true;
      this.title = "添加报损单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReporting(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报损单";
      });
    },
    confirmReporting(row){
      let _that = this
      var t = row
      this.$modal.confirm('是否确认报损单').then(function() {
        confirmReporting(t).then(res =>{
          if (res.code == 200){
            _that.$modal.msgSuccess("报损成功");
            _that.getList();
          }
        })
      })
    },
    typeCheck(row){
      if (row.type == 0){
        return "已确认"
      }else {
        return "未确认"
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReporting(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReporting(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除报损单编号为"' + ids + '"的数据项？').then(function() {
        return delReporting(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/reporting/export', {
        ...this.queryParams
      }, `reporting_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
